package handler

import (
	"car-order-pro/cache"
	"car-order-pro/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	CarCode := c.PostForm("CarCode")
	CarName := c.PostForm("CarName")
	CarType := c.PostForm("CarType")
	data := model.Car{
		CarCode: CarCode,
		CarName: CarName,
		CarType: CarType,
	}
	err := model.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarOrder(c *gin.Context) {
	OrderCode := c.PostForm("OrderCode")
	OrderFrom := c.PostForm("OrderFrom")
	OrderStatus := c.PostForm("OrderStatus")
	Cid, _ := strconv.Atoi(c.PostForm("Cid"))
	data := model.CarOrder{
		OrderCode:   OrderCode,
		OrderFrom:   OrderFrom,
		OrderStatus: OrderStatus,
		Cid:         Cid,
	}
	err := model.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarOrderList(c *gin.Context) {
	redis_key := "list"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	var carOrderList []model.CarOrderList
	/*
		SELECT
		  car_orders.id AS OrderId,
		  car_orders.order_code AS OrderCode,
		  car_orders.order_from AS OrderFrom,
		  car_orders.order_status AS OrderStatus,
		  cars.car_code AS CarCode,
		  cars.car_name AS CarName,
		  cars.car_type as CarType
		FROM
		  car_orders
		  JOIN cars ON car_orders.cid = cars.id
	*/
	if cacheData == "" {
		model.DB.Debug().Table("car_orders").
			Select("car_orders.id AS OrderId",
				"car_orders.order_code AS OrderCode",
				"car_orders.order_from AS OrderFrom",
				"car_orders.order_status AS OrderStatus",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_type as CarType").
			Joins("JOIN cars ON car_orders.cid = cars.id").
			Find(&carOrderList)
		jsonData, _ := json.Marshal(&carOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrderList)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": carOrderList,
	})
	return
}

func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	redis_key := "list_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	var carOrderList model.CarOrderList
	/*
		SELECT
		  car_orders.id AS OrderId,
		  car_orders.order_code AS OrderCode,
		  car_orders.order_from AS OrderFrom,
		  car_orders.order_status AS OrderStatus,
		  cars.car_code AS CarCode,
		  cars.car_name AS CarName,
		  cars.car_type as CarType
		FROM
		  car_orders
		  JOIN cars ON car_orders.cid = cars.id
	*/
	if cacheData == "" {
		model.DB.Debug().Table("car_orders").
			Select("car_orders.id AS OrderId",
				"car_orders.order_code AS OrderCode",
				"car_orders.order_from AS OrderFrom",
				"car_orders.order_status AS OrderStatus",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_type as CarType").
			Joins("JOIN cars ON car_orders.cid = cars.id").
			Where("car_orders.id=?", id).
			Find(&carOrderList)
		jsonData, _ := json.Marshal(&carOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrderList)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": carOrderList,
	})
	return
}
